const http = require("http");
const fs = require("fs");
//安装cheerio模块，三方模块。类似jq来操作html

const cheerio = require("cheerio")

let weburl ="http://www.atguigu.com/";
http.get(weburl,req=>{
    let str ="";
    req.on('data',chunk=>{
        str+= chunk;
    })
    req.on('end',()=>{
        // console.log(str)
        // fs.writeFileSync('./index.html',str);
        let $ = cheerio.load(str)
        let arr =[]
        let num =1
        $('.news .con .rr li a').each((key,item )=>{
            let txt = $(item).text();
            let obj={
                id:num,
                content:txt
            }

            arr.push(obj)
            num++
        })
        fs.writeFileSync('./data.json',JSON.stringify(arr))
    })

})